package com.ruoyi.web.controller.share;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.share.domain.ShareViwepager;
import com.ruoyi.share.service.IShareViwepagerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 轮播图Controller
 *
 * @author ruoyi
 * @date 2021-11-16
 */
@RestController
@RequestMapping("/api/shoreviwepager")
public class ShareViwepagerController extends BaseController
{
    @Autowired
    private IShareViwepagerService shareViwepagerService;

    /**
     * 查询轮播图列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ShareViwepager shareViwepager)
    {
        List<ShareViwepager> list = shareViwepagerService.selectShareViwepagerList(shareViwepager);
        return getDataTable(list);
    }

    /**
     * 导出轮播图列表
     */

    @GetMapping("/export")
    public AjaxResult export(ShareViwepager shareViwepager)
    {
        List<ShareViwepager> list = shareViwepagerService.selectShareViwepagerList(shareViwepager);
        ExcelUtil<ShareViwepager> util = new ExcelUtil<ShareViwepager>(ShareViwepager.class);
        return util.exportExcel(list, "轮播图数据");
    }

    /**
     * 获取轮播图详细信息
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(shareViwepagerService.selectShareViwepagerById(id));
    }

    /**
     * 新增轮播图
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ShareViwepager shareViwepager)
    {
//        System.err.println(shareViwepager);
        return toAjax(shareViwepagerService.insertShareViwepager(shareViwepager));
    }

    /**
     * 修改轮播图
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody ShareViwepager shareViwepager)
    {
        return toAjax(shareViwepagerService.updateShareViwepager(shareViwepager));
    }

    /**
     * 删除轮播图
     */
	@DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shareViwepagerService.deleteShareViwepagerByIds(ids));
    }


    @PostMapping("/multiPicturesUpload")
    public AjaxResult multiPicturesUpload(MultipartFile file) throws IOException {
        //RuoYiConfig.getForumPath() 获取自定义该模块的图片保存路径
        if (!file.isEmpty()){
            //获取上传成功的图片保存路径，并返回给前台
            String picture = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
//            System.err.println(picture);
            if (!StringUtils.isEmpty(picture)){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", picture);
                return ajax;
            }
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }
}
